package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.RequiredPermission;
import com.sun.deploy.net.HttpResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RequiredPermission("xxx")
@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

@RequiresPermissions(value = {"员工列表", "employee:list"},logical = Logical.OR)
@RequestMapping("/list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
        model.addAttribute("pageInfo", employeeService.query(qo));
        model.addAttribute("depts", departmentService.list());
        return "employee/list";
    }

@RequiresPermissions(value = {"员工编辑", "employee:input"},logical = Logical.OR)
@RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("entity", employeeService.get(id));
        }
        //查询所有部门
        model.addAttribute("depts", departmentService.list());
        //查询所有角色
        model.addAttribute("roles", roleService.list());
        return "employee/input";
    }

@RequiresPermissions(value = {"员工保存或更新", "employee:saveOrUpdate"},logical = Logical.OR)
@RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee entity, Long[] ids) {
        JsonResult result = new JsonResult();
        try {

            employeeService.saveOrUpdate(entity, ids);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("亲,操作失败!");
        }
        return result;
    }

@RequiresPermissions(value = {"员工删除", "employee:delete"},logical = Logical.OR)
@RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try {

            employeeService.delete(id);
        }catch (Exception e)
        {
            result.mark("亲,删除失败");
        }
        return result;
    }
    @RequiresPermissions(value = {"员工批量删除", "employee:batchDelete"},logical = Logical.OR)
    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids)
    {
        JsonResult result = new JsonResult();
        try {

            employeeService.batchDelete(ids);
        }catch (Exception e)
        {
            result.mark("亲,删除失败");
        }
        return result;
    }
    @RequiresPermissions(value = {"员工导出", "employee:exportExcel"},logical = Logical.OR)
    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response)
    {
        //创建工作簿
        HSSFWorkbook book =  employeeService.exportExcel();;

        //设置下载文件的名称
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        try {
            book.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequiresPermissions(value = {"员工导入", "employee:importXls"},logical = Logical.OR)
    @RequestMapping("/importXls")
    public String importXls(MultipartFile file)
    {
        employeeService.importXls(file);
        return "redirect:/employee/list.do";
    }


}
